<?php
/**
 * 代售会员卡
 * User: ws
 * Date: 2018/10/15
 * Time: 11:10
 */

namespace app\merchant\controller;
use app\admin\controller\Suser;
use think\Db;

class Offline extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['s.name|c.name|o.sales_order'] = array('like','%'.trim($key).'%');
            $where['o.store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $list = Db::table('je_card_offline')->alias('o')->join('je_membership_card c','o.card_id = c.card_id')->join('je_store_user s','o.store_user_id = s.store_user_id')->field('o.* ,c.name as cname,s.name as sname')->order('offline_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach($list['data'] as $key=>$item){
                $list['data'][$key]['card'] = '';
                $userCard = Db::table('je_user_card_log')->alias('l')->join('je_user u','l.user_id = u.user_id')->field('u.name as name ,l.purchase_time,l.start_time,l.validity_time')->where("l.offline_id = {$item['offline_id']}")->find();
                $card = array('name'=>'','purchase_time'=>'','start_time'=>'','validity_time'=>'');
                $list['data'][$key]['card'] = $userCard?$userCard:$card;
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $data['store_id'] = $_SESSION['think']['admin_user']['store_id'];
            $data['create_time'] = time();
            Db::startTrans();
            try{
                Db::name('card_offline')->insert($data);
                $offline_id = Db::name('card_offline')->getLastInsID();

                $wx = Db::name('wx_config')->where("config_id=1")->find();
                $time = time();
                if($wx['date']<time()){
                    $suser = new Suser();
                    $wx['access_token'] = $suser->wx_acc();
                }
                $access = $wx['access_token'];
                qrcode($access,$offline_id,1);
                $qrcode = "/uploads/qrcode/offline/$offline_id.png";
                Db::table('je_card_offline')->where('offline_id',$offline_id)->setField('qrcode', $qrcode);
                Db::commit();

                return json(array('code'=>200,'msg'=>'添加成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(array('code'=>0,'msg'=>$e->getMessage()));
            }

        }
        $where['store_id'] = $_SESSION['think']['admin_user']['store_id'];
        $card = db('membership_card')->field('type,name,img,price,card_id')->where('state=1')->order('sort desc ,card_id desc')->select();
        $store = db('store_user')->field('store_user_id,name')->order('store_user_id desc')->where($where)->select();

        return view('add',['card'=>$card,'store'=>$store]);
    }

    function del(){
        $id = input('param.id');
        $user_card_log = Db::name('user_card_log')->field('offline_id')->where("offline_id={$id}")->find();
        $card_order = Db::name('card_order')->field('offline_id')->where("offline_id={$id}")->find();
        if($user_card_log && $card_order){
            return json(array('code'=>0,'msg'=>'删除失败，已被绑定不能为删除'));
        }
        $re = db('card_offline')->delete($id);
        if (!$re) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        unlink('.'."/uploads/qrcode/offline/$id.png");
        return json(array('code'=>200,'msg'=>'删除成功'));
    }



    function card(){
        $id = input('id');
        $card = db('membership_card')->field('type,name,img,price,card_id')->where("card_id = {$id}")->find();

        $url = config('aliyun_oss.url');
        $img = str_replace("/card",$url."card",$card['img']);

        if($card['type']==0){$type = '年卡';}elseif ($card['type']==1){$type = '半年卡';}elseif ($card['type']==2){$type = '季卡';}elseif ($card['type']==3){$type = '月卡';}

        $html = ' <div class="layui-input-inline a" style="width:300px;height: 100px;background: url('.$img.')">';
        $html .= '<p>'.$card['name'].'     <span style="float: right;"> '.$type.'</span></p>';
        $html .= '<p style="color: #FF0000; padding-top: 20%;float: right;">￥'.$card['price'].'</p>';
        $html .= '</div>';

        echo $html;exit();
    }
}